package array;

public class GreaterThanGivenNumber {

	// Points:
	// 1. repeating values are present
	// 2. k may not be present in array
	
	public static int findGreaterThanGivenNumber(int[] arr, int k){
		
		int begin = 0;
		int end = arr.length - 1;
		
		while(begin <= end){
			
			int mid = begin + (end - begin)/2;
			
			// corner case when mid is last index as mid+1 will not exists
			if(mid == arr.length - 1){
				return Integer.MIN_VALUE;
			}else if(mid == 0){ // corner case when key is smaller than the smallest number in array 
				return arr[0];
			}
			
			if(arr[mid] <= k && arr[mid+1] >k){
				return arr[mid+1];
			}else if(arr[mid] <= k){
				begin = mid + 1;
			}else{
				//end = mid - 1;
				end = mid;
			}
		}
		
		return Integer.MIN_VALUE;
	} // functionality ends	
	
}
